﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Battleship
{
    public partial class ContainerGenerator : Form
    {
        LoadingPlan planning = new LoadingPlan();
        public ContainerGenerator()
        {
            InitializeComponent();

        }
        public ContainerGenerator(ref LoadingPlan iPlanning)
        {
            InitializeComponent();
            planning = iPlanning;

            ColectionDictionary colection = new ColectionDictionary();

            foreach (Port item in planning.portList.content)
            {
                colection.conPOO.Add(item.PI,item.PN);
            }

            conType.DataSource = new BindingSource(colection.conType, null);
            conType.DisplayMember = "Key";
            conType.ValueMember = "Value";

            conSize.DataSource = new BindingSource(colection.conSize, null);
            conSize.DisplayMember = "Key";
            conSize.ValueMember = "Value";

            conPOO.DataSource = new BindingSource(colection.conPOO, null);
            conPOO.DisplayMember = "Value";
            conPOO.ValueMember = "Key";

            conPOD.DataSource = new BindingSource(colection.conPOO, null);
            conPOD.DisplayMember = "Value";
            conPOD.ValueMember = "Key";

            

        }

        private void txtWeight_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private void txtL_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private void txtW_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private void txtH_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ContainerGenerator_Load(object sender, EventArgs e)
        {
            
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {

            ContainerInfo containerInfo = new ContainerInfo();
            containerInfo.Name = conName.Text;
            containerInfo.Size = Int32.Parse(conSize.SelectedValue.ToString());
            containerInfo.Type = conType.SelectedValue.ToString();
            containerInfo.POO = new Port(conPOO.Text.ToString(), Int32.Parse(conPOO.SelectedValue.ToString()));
            containerInfo.POD = new Port(conPOD.Text.ToString(), Int32.Parse(conPOD.SelectedValue.ToString()));
            containerInfo.Weight = float.Parse(conWeight.Text);
            containerInfo.LCor = new Cor(txtL.Text, txtW.Text, txtH.Text);
            if (cbxCorrupted.Checked==true)
            {
                containerInfo.isCorrupted = true;
            }
            planning.addContainer(containerInfo);

            this.Close();
        }
    }
}
